package cn.ht.jywrwater.module.ht.controller.admin.waterpipenet;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.waterpipenet.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterpipenet.WaterPipenetDO;
import cn.ht.jywrwater.module.ht.convert.waterpipenet.WaterPipenetConvert;
import cn.ht.jywrwater.module.ht.service.waterpipenet.WaterPipenetService;

@Tag(name = "管理后台 - 管网")
@RestController
@RequestMapping("/ht/water-pipenet")
@Validated
public class WaterPipenetController {

    @Resource
    private WaterPipenetService waterPipenetService;

    @PostMapping("/create")
    @Operation(summary = "创建管网")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:create')")
    public CommonResult<Long> createWaterPipenet(@Valid @RequestBody WaterPipenetCreateReqVO createReqVO) {
        return success(waterPipenetService.createWaterPipenet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新管网")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:update')")
    public CommonResult<Boolean> updateWaterPipenet(@Valid @RequestBody WaterPipenetUpdateReqVO updateReqVO) {
        waterPipenetService.updateWaterPipenet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除管网")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:delete')")
    public CommonResult<Boolean> deleteWaterPipenet(@RequestParam("id") Long id) {
        waterPipenetService.deleteWaterPipenet(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得管网")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:query')")
    public CommonResult<WaterPipenetRespVO> getWaterPipenet(@RequestParam("id") Long id) {
        WaterPipenetDO waterPipenet = waterPipenetService.getWaterPipenet(id);
        return success(WaterPipenetConvert.INSTANCE.convert(waterPipenet));
    }

    @GetMapping("/list")
    @Operation(summary = "获得管网列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:query')")
    public CommonResult<List<WaterPipenetRespVO>> getWaterPipenetList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterPipenetDO> list = waterPipenetService.getWaterPipenetList(ids);
        return success(WaterPipenetConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得管网分页")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:query')")
    public CommonResult<PageResult<WaterPipenetRespVO>> getWaterPipenetPage(@Valid WaterPipenetPageReqVO pageVO) {
        PageResult<WaterPipenetDO> pageResult = waterPipenetService.getWaterPipenetPage(pageVO);
        return success(WaterPipenetConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出管网 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterPipenetExcel(@Valid WaterPipenetExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterPipenetDO> list = waterPipenetService.getWaterPipenetList(exportReqVO);
        // 导出 Excel
        List<WaterPipenetExcelVO> datas = WaterPipenetConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "管网.xls", "数据", WaterPipenetExcelVO.class, datas);
    }

    @GetMapping("/position")
    @Operation(summary = "获得管网位置信息")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet:query')")
    public CommonResult<List<WaterPipenetRespVO>> getWaterPipenetosition() {
        List<WaterPipenetDO> list = waterPipenetService.getWaterPipenetosition();
        return success(WaterPipenetConvert.INSTANCE.convertList(list));
    }

}
